package Verktoy;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * Klasse som passer på at man har med alt man skal ha i et passord
 * @author Gruppe1
 */
@FacesValidator(value="passord")
public class PassordValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String passord1 = (String) value;
        int tall = 0;
        int bokst = 0;
        int spess = 0;
        for(int i = 0 ; i < passord1.length() ; i++){
            char a = passord1.charAt(i);
            if((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z')) bokst++;
            else if(a <= '9' && a >= '0') tall++;
            else spess++;
        }
        if(tall == 0) throw new ValidatorException(new FacesMessage("Ingen tall"));
        if(bokst == 0) throw new ValidatorException(new FacesMessage("Ingen bokstaver"));
        if(spess == 0) throw new ValidatorException(new FacesMessage("Ingen spesialtegn"));
        if(passord1.length() < 6) throw new ValidatorException(new FacesMessage("For kort passord"));
        throw new ValidatorException(new FacesMessage("Passord OK"));
    }
}
